local UIBase = require("view.UIBase")
local JoyStick = class("JoyStick", UIBase)
local Vector3 = require("utils.Vector3")
local GameUtils = require("utils.GameUtils")
local Vector2 = require("utils.Vector2")

local MAX = 54

function JoyStick:ctor()
    local resource = {
        path = "Prefabs/View/JoyStick",
        name = "JoyStick",
        canvas = 1
    }
    JoyStick.super.ctor(self, resource)
    self.horizontal = 0
    self.vertical = 0
end

function JoyStick:start()
    self._initPos = self._joyBg.rectTransform.localPosition
    self._uicamera = G_UIManager:getUICamera()
    self._imageJoy.rectTransform.localPosition = Vector3.new(0, 0, 0)
    -- self._panel:SetCallback(handler(self, self._onPanelTouch))
end

function JoyStick:hide(hide)
    if hide then
        self._joyBg.rectTransform.localPosition = self._initPos
        self._imageJoy.rectTransform.localPosition = Vector3.new(0, 0, 0)
        self.horizontal = 0
        self.vertical = 0
        self._start = false
        self.gameObject:SetActive(false)
    else
        self.gameObject:SetActive(true)
    end
end

function JoyStick:_windowsInput()
    if G_Input.GetMouseButtonDown(0) then
        local mouPos = G_Input.mousePosition
        local pos = Vector2.new(mouPos.x, mouPos.y)
        if GameUtils.getFirstPickGameObject(pos) then
            return
        end
        local outPos = GameUtils.converScreenPosToUIPos(pos, self._rectTransform)
        if outPos.x < -100 and outPos.y < 50 then
            self._start = true
            self._joyBg.rectTransform.localPosition = Vector3.new(outPos.x, outPos.y, 0)
        end
    end
    if self._start then
        local mouPos = G_Input.mousePosition
        local position = Vector2.new(mouPos.x, mouPos.y) -- G_Input.position
        local outPos = GameUtils.converScreenPosToUIPos(position, self._joyBg.rectTransform)
        outPos.x = math.max(outPos.x, -54)
        outPos.x = math.min(outPos.x, 54)
        outPos.y = math.max(outPos.y, -54)
        outPos.y = math.min(outPos.y, 54)
        self.horizontal = outPos.x / MAX
        self.vertical = outPos.y / MAX
        self._imageJoy.rectTransform.localPosition = Vector3.new(outPos.x, outPos.y, 0)
    end
    if G_Input.GetMouseButtonUp(0) then
        self._start = false
        self._joyBg.rectTransform.localPosition = self._initPos
        self._imageJoy.rectTransform.localPosition = Vector3.new(0, 0, 0)
        self.horizontal = 0
        self.vertical = 0
    end
end

function JoyStick:update(dt)
    if
        Application.platform == CS.UnityEngine.RuntimePlatform.Android or
            Application.platform == CS.UnityEngine.RuntimePlatform.IPhonePlayer
     then
        self:_mobileInout()
    else
        self:_windowsInput()
    end
end

function JoyStick:_mobileInout()
    if G_Input.touchCount == 0 then
        return
    end
    local touch = G_Input.GetTouch(0)
    if touch.phase == CS.UnityEngine.TouchPhase.Began then
        local pos = touch.position
        if GameUtils.getFirstPickGameObject(pos) then
            return 
        end
        local outPos = GameUtils.converScreenPosToUIPos(pos, self._rectTransform)
        if outPos.x < -100 and outPos.y < 50 then
            self._start = true
            self._joyBg.rectTransform.localPosition = Vector3.new(outPos.x, outPos.y, 0)
        end
    end
    if touch.phase == CS.UnityEngine.TouchPhase.Moved then
        if self._start then
            local position = G_Input.position
            local outPos = GameUtils.converScreenPosToUIPos(position, self._joyBg.rectTransform)
            outPos.x = math.max(outPos.x, -54)
            outPos.x = math.min(outPos.x, 54)
            outPos.y = math.max(outPos.y, -54)
            outPos.y = math.min(outPos.y, 54)
            self.horizontal = outPos.x / MAX
            self.vertical = outPos.y / MAX
            self._imageJoy.rectTransform.localPosition = Vector3.new(outPos.x, outPos.y, 0)
        end
    end
    if touch.phase == CS.UnityEngine.TouchPhase.Canceled or touch.phase == CS.UnityEngine.TouchPhase.Ended then
        self._start = false
        self._joyBg.rectTransform.localPosition = self._initPos
        self._imageJoy.rectTransform.localPosition = Vector3.new(0, 0, 0)
        self.horizontal = 0
        self.vertical = 0
    end
end

function JoyStick:_touchBegin(event)
end

function JoyStick:_touchMove(event)
    printDebug(event.position)
end

function JoyStick:_toucnEnd(event)
end

function JoyStick:_touchCancel(event)
end

return JoyStick
